# -*- coding: utf-8 -*-
# @Time    : 2021/12/7 19:52
# @Author  : tuhou
# @File    : zhihu.py
# @Software: PyCharm
import requests
import execjs
import time


url = "https://www.zhihu.com/api/v4/search_v3?t=general&q=python&correction=1&offset=20&limit=20&filter_fields=&lc_idx=0&show_all_topics=0&search_source=Normal"

headers = {
  # 'authority': 'www.zhihu.com',
  # 'pragma': 'no-cache',
  # 'cache-control': 'no-cache',
  'x-zse-93': '101_3_2.0',
  # 'x-ab-param': 'top_test_4_liguangyi=1;tp_topic_style=0;se_ffzx_jushen1=0;zr_slotpaidexp=1;tp_contents=2;pf_noti_entry_num=2;qap_question_visitor= 0;tp_dingyue_video=0;tp_zrec=1;qap_question_author=0;zr_expslotpaid=1;pf_adjust=1',
  # 'x-ab-pb': 'CtwBNwz4A1YM9AvoBoQC4AQYBpQGmwsbAIwEEQUHDFEFUgWrBtcCOQZPA7IFMAbbBs8LCgQLBNoEogbcBvoGRQQxBjQEpgQBC+wKQwD0AxUFMgWeBTcF5wXcCzIDDgVABokMtAB9Aj8GrAYcBn4GdQSNBFUF2AUqBusG4wVqAaEDGQV8BoAFiwVbBqIDFAUzBRYGKQU/AEIEwgVBBlILRwBQA7ULdAGmBsYG0Qb2AukEzAJXBA8LNAzgC7kCoANcBgcH1wsBBrQKaQE7AvMDMwSMBWAL2AK3A0AB5ApWBRJuAQEBAAAAAgAAAAEEAAEBAAAAAAAAAAALAQABAAAAAAAAAAABFQACAAEAAAAAAAAAAAEAAAAAAAQAAAABAAAAAAAAAAEAAAIAAAADAgABAAADAAAAAQABAAEBAAAVAAAAAAAAAAAAAAAAAAABAAA=',
  # 'x-api-version': '3.0.91',
  # 'sec-ch-ua-mobile': '?0',
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36',
  # 'x-requested-with': 'fetch',
  'x-zse-96': '2.0_aXOBkHL8QLtxb_OyKLt0Hc9qkXtYFCtyM7t0cHH0nG2f',
  # 'x-app-za': 'OS=Web',
  # 'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
  # 'sec-ch-ua-platform': '"Windows"',
  # 'accept': '*/*',
  # 'sec-fetch-site': 'same-origin',
  # 'sec-fetch-mode': 'cors',
  # 'sec-fetch-dest': 'empty',
  # 'referer': 'https://www.zhihu.com/search?type=content&q=python',
  # 'accept-language': 'zh-CN,zh;q=0.9',
  'cookie': ''
}

with open('zhihu.js', encoding="utf-8") as f:
  exec_js = f.read()

r = "101_3_2.0+"
c = url[21:]
# uuid = '+"ADDe_b-j_BKPTtohwaRGl2dR_yr2ZULNrKM=|"' + str(int(time.time()))
uuid = '+"ADDe_b-j_BKPTtohwaRGl2dR_yr2ZULNrKM=|' + '1618972005"'

s = r + c + uuid
#'101_3_2.0+/api/v4/search/top_search+"ADDe_b-j_BKPTtohwaRGl2dR_yr2ZULNrKM=|1618972005"'

js = execjs.compile(exec_js)
s = js.call("get_s", s)

headers['x-zse-96'] = s
response = requests.request("GET", url, headers=headers)

print(response.json())

